<template>
  <div>
    <!-- 显示当前评论的cell -->
    <van-cell>
      <template #title>
        <div class="cmt-cell">
          <div class="left-box">
            <van-image fit="cover" round :src="item.aut_photo" />

            <div class="info-box">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div>
                <span class="time">{{ $relvTime(item.pubdate) }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </van-cell>

    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id" :title="item.content">
        <template #title>
          <div class="cmt-cell">
            <div class="left-box">
              <van-image fit="cover" round :src="item.aut_photo" />

              <div class="info-box">
                <div class="name">{{ item.aut_name }}</div>
                <div class="content">{{ item.content }}</div>
                <div>
                  <span class="time">{{ $relvTime(item.pubdate) }}</span>
                </div>
              </div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>

    <div class="field-box">
      <van-field
        @keyup.enter="send"
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { commentAPI, sendAPI } from '@/api/comment'
export default {
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmt: '',
      offset: null,
      replyShow: false,
      currentItem: {}
    }
  },
  methods: {
    clickReply (item) {
      // 显示出回复面板
      this.replyShow = true
      // 点谁就拿到谁存到currentItem
      // currenItem就会自动流入到子
      this.currentItem = item
    },
    async send () {
      // 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')
      const res = await sendAPI({
        target: this.item.com_id,
        content: this.cmt,
        // 发表回复要携带文章id
        art_id: this.$store.state.params.id
      })
      // console.log(res)
      // 把添加后的评论放到数组里即可
      this.list.unshift(res.data.new_obj)
      // 输入框清空
      this.cmt = ''
      // 要让数量+1 -- 通知父组件来做
      this.$emit('add')
    },
    async onLoad () {
      const res = await commentAPI({
        // 因为现在获取的是回复，就要传c
        type: 'c',
        // 获取评论这里传评论id
        source: this.item.com_id,
        offset: this.offset,
        // 一次查10条
        limit: 10
      })

      // 把本次返回的最后一条存到offset里
      // 作为下一次查询的开始
      this.offset = res.data.last_id
      this.list.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false

      // 判断是否全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
